package com.zoe.im.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Zoe.Zhang
 * @date 2021/11/02
 * @description
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);

    }

    // @Bean
    // CorsConfigurationSource corsConfigurationSource() {
    //     UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    //     CorsConfiguration corsConfiguration = new CorsConfiguration();
    //     //同源配置，*表示任何请求都视为同源，若需指定ip和端口可以改为如“localhost：8080”，多个以“，”分隔；
    //     corsConfiguration.setAllowCredentials(true);
    //     corsConfiguration.addAllowedOrigin("*");
    //     //header，允许哪些header，本案中使用的是token，此处可将*替换为token；
    //     corsConfiguration.addAllowedHeader("*");
    //     //允许的请求方法，POST、GET等
    //     corsConfiguration.addAllowedMethod("*");
    //     urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
    //
    //     return urlBasedCorsConfigurationSource;
    // }

}
